<?php

namespace App\Http\Controllers\Api;

use App\Cart;
use App\Goods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $carts = Cart::where('user_id', Auth::id())->whereHas('goods')->with(['goods.underwayFlashSale', 'goods.specPrices', 'spec'])->orderBy('id', 'desc')->limit(20)->get();
        $carts->map(function($item, $key) use ($carts) {
            if ($item->goods->specPrices->count() > 0 && !$item->spec) {
                // 删除数据异常的条目，防止商品编辑后，选购的商品规格出现错误的情况
                $item->delete();
                unset($carts[$key]);
            }
        });
        return $carts;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user_id = Auth::id();
        $specGoodsPriceId = $request->get('spec_goods_price_id', 0);
        $quantity = $request->get('quantity', 1);
        $product = Goods::findOrfail($request->goods_id);

        if ($product->specPrices->count() > 0 && !$specGoodsPriceId) {
            return response(['message' => '请选择商品规格'], 400);
        }
        // 判断购物车中是否与存在相同商品
        $cart = Cart::where('goods_id', $request->goods_id)->where('user_id', $user_id)->where('spec_goods_price_id', $specGoodsPriceId)->first();
        if ($cart) {
            $cart->increment('quantity', $quantity);
        } else {
            $cart = Cart::create([
                'quantity' => $quantity,
                'goods_id' => $request->get('goods_id'),
                'spec_goods_price_id' => $specGoodsPriceId,
                'user_id' => $user_id
            ]);
        }
        return $cart;
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Cart  $cart
     * @return \Illuminate\Http\Response
     */
    public function show(Cart $cart)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Cart  $cart
     * @return \Illuminate\Http\Response
     */
    public function edit(Cart $cart)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Cart  $cart
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Cart $cart)
    {
        $quantity = $request->get('quantity', 0);
        $stock = $cart->goods->stock;
        if ($cart->spec) {
            $stock = $cart->spec->stock;
        }
        if ($quantity <= 0) {
            return response(['message' => '购买数量不能小于1'],400);
        }
        if ($quantity > $stock) {
            return response(['message' => '库存不足'], 400);
        }
        $cart->update($request->all());
        return response(['message' => '修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Cart  $cart
     * @return \Illuminate\Http\Response
     */
    public function destroy(Cart $cart)
    {
        $cart->delete();
        return response(['message' => '删除成功']);
    }
}
